<template>
  <div class="userList">
    <div class="card">
      <el-card>
        <el-form :inline="true" :model="form" class="demo-form-inline">
          <el-form-item label="用户名称">
            <el-input v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="用户邮箱">
            <el-input v-model="form.email"></el-input>
          </el-form-item>
          <el-form-item label="角色">
            <el-select v-model="form.role_id" placeholder="请选择">
              <el-option
                v-for="(value, key, index) in roleObj"
                :key="index"
                :label="value"
                value="key"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">搜索</el-button>
            <el-button @click="onClear">清除</el-button>
            <el-button type="primary" @click="addEvent">+新增用户</el-button>
          </el-form-item>
        </el-form>
      </el-card>
      <el-card>
        <el-form>
          <el-table :data="list" style="width: 100%">
            <el-table-column prop="number" label="序号" width="60">
              <template v-slot="{ $index }">
                {{ (form.page - 1) * form.limit + $index + 1 }}
              </template>
            </el-table-column>
            <el-table-column prop="username" label="用户名" width="180">
            </el-table-column>
            <el-table-column prop="phone" label="手机号" width="180">
            </el-table-column>
            <el-table-column prop="email" label="邮箱" width="180">
            </el-table-column>
            <el-table-column prop="role" label="角色" width="180">
            </el-table-column>
            <el-table-column prop="remark" label="备注"> </el-table-column>
            <el-table-column prop="status" label="状态" width="100">
              <template v-slot="{ row }">
                {{ row.status == 0 ? '禁用' : '启用' }}
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="150">
              <template v-slot="{ row }">
                <el-button @click="editClick(row)" type="text" size="small"
                  >编辑</el-button
                >
                <el-button
                  @click="statusClick(row.id)"
                  type="text"
                  size="small"
                  >{{ row.status == 1 ? '禁用' : '启用' }}</el-button
                >
                <el-button type="text" size="small" @click="del(row.id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </el-card>
      <el-card>
        <div class="block">
          <el-pagination
            @size-change="sizeChange"
            @current-change="pageChange"
            :current-page="page.page"
            :page-sizes="[1, 5, 10, 20, 50, 100]"
            :page-size="page.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
          >
          </el-pagination>
        </div>
      </el-card>
    </div>
    <!-- 新增用户组件 -->
    <Add ref="add" @getData="getData" />
    <!-- 编辑页面组件 -->
    <Edit ref="edit" :roleObj="roleObj" @getData="getData" />
  </div>
</template>

<script>
import { userList, userStatus, userRemove } from '@/api/userList.js'
import Add from './components/Add.vue'
import Edit from './components/Edit.vue'
import cookieJs from 'js-cookie'
export default {
  components: {
    Add,
    Edit
  },
  data () {
    return {
      show: true,
      list: [{}],
      roleObj: {
        1: '超级管理员',
        2: '管理员',
        3: '老师',
        4: '学生'
      },
      form: {
        username: '',
        email: '',
        role_id: '',
        page: '',
        limit: ''
      },
      page: {
        total: 100,
        limit: +cookieJs.get('limit') || 5,
        page: 1
      }
    }
  },
  created () {
    this.getData()
  },
  methods: {
    // 删除按钮的点击事件
    del (id) {
      userRemove({ id }).then(() => {
        this.$message.success('删除成功')
        // 刷新数据
        this.getData()
      })
    },
    // 设置状态
    statusClick (id) {
      userStatus({ id }).then(() => {
        // 提示设置成功
        this.$message.success('设置状态成功')
        // 刷新数据
        this.getData()
      })
    },
    // 新增点击事件
    addEvent () {
      this.$refs.add.isShow = true
    },
    // 编辑按钮的点击事件
    editClick (row) {
      this.$refs.edit.form = JSON.parse(JSON.stringify(row))
      // console.log(this.$refs.edit.form)
      this.$refs.edit.isShow = true
    },
    // 搜索按钮的点击事件
    onSubmit () {
      this.getData()
    },
    // 清除的点击事件
    onClear () {
      this.form.username = ''
      this.form.email = ''
    },
    sizeChange (size) {
      this.page.limit = size
      cookieJs.set('limit', size)
      this.page.page = 1
      this.getData()
    },
    pageChange (page) {
      this.form.page = page
      this.getData()
    },
    // 获取数据
    async getData () {
      const res = await userList(this.form)
      // console.log('用户列表', res)
      this.list = res.data.items
      this.page.total = res.data.pagination.total
      this.page.page = +res.data.pagination.page
    }
  }
}
</script>

<style lang="less" scoped>
.userList {
  padding: 10px;
  .block {
    border-top: 1px solid #ebeef5;
    padding-top: 10px;
    text-align: center;
  }
  .handle {
    margin-right: 20px;
    color: #409eff;
    cursor: pointer;
  }
}
</style>
